<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>RSA的C++语言描述简单实现 | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言网络安全中RSA的C++语言描述简单实现。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   代码特点纯C++语言：  相对规范和整洁 一定程度地面向对象 使用一部分高级特性 考虑优化性能  详细注释：  提示规范和整洁 提示面向对象 提示高">
<meta property="og:type" content="article">
<meta property="og:title" content="RSA的C++语言描述简单实现">
<meta property="og:url" content="http://example.com/2022/11/09/RSA%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言网络安全中RSA的C++语言描述简单实现。  代码仓库 yezhening&#x2F;Programming-examples: 编程实例 (github.com) Programming-examples: 编程实例 (gitee.com)   代码特点纯C++语言：  相对规范和整洁 一定程度地面向对象 使用一部分高级特性 考虑优化性能  详细注释：  提示规范和整洁 提示面向对象 提示高">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover15.png">
<meta property="article:published_time" content="2022-11-09T12:36:40.000Z">
<meta property="article:modified_time" content="2023-04-26T07:45:57.692Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="网络安全">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover15.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2022/11/09/RSA%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'RSA的C++语言描述简单实现',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-26 15:45:57'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover15.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">RSA的C++语言描述简单实现</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-09T12:36:40.000Z" title="发表于 2022-11-09 20:36:40">2022-11-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-26T07:45:57.692Z" title="更新于 2023-04-26 15:45:57">2023-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/">网络安全</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="RSA的C++语言描述简单实现"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>网络安全中RSA的C++语言描述简单实现。</p>
<hr>
<h1 id="代码仓库"><a href="#代码仓库" class="headerlink" title="代码仓库"></a>代码仓库</h1><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/yezhening/Programming-examples">yezhening&#x2F;Programming-examples: 编程实例 (github.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://gitee.com/yezhening/Programming-examples">Programming-examples: 编程实例 (gitee.com)</a></li>
</ul>
<hr>
<h1 id="代码特点"><a href="#代码特点" class="headerlink" title="代码特点"></a>代码特点</h1><p>纯C++语言：</p>
<ul>
<li>相对规范和整洁</li>
<li>一定程度地面向对象</li>
<li>使用一部分高级特性</li>
<li>考虑优化性能</li>
</ul>
<p>详细注释：</p>
<ul>
<li>提示规范和整洁</li>
<li>提示面向对象</li>
<li>提示高级特性</li>
<li>提示优化性能</li>
<li>解析RSA步骤（<strong>网络上大部分实现代码的含义不明确，本代码相对明确</strong>）</li>
<li>注意易错点</li>
</ul>
<hr>
<h1 id="大（素）数讨论"><a href="#大（素）数讨论" class="headerlink" title="大（素）数讨论"></a>大（素）数讨论</h1><ul>
<li>实际的RSA需要操作大（素）数</li>
<li>因为大（素）数结合RSA的代码实现较复杂，所以本代码未实现大（素）数部分，（简单）实现RSA部分</li>
<li>网络上有很多大数实现的思路和代码资料，有兴趣可以参阅</li>
</ul>
<h2 id="部分资料"><a href="#部分资料" class="headerlink" title="部分资料"></a>部分资料</h2><ul>
<li><a target="_blank" rel="noopener" href="https://ask.csdn.net/questions/7514078">生成1024比特随机数-编程语言-CSDN问答</a></li>
<li><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/1e139541c4eb">RSA 大数的处理 - 简书 (jianshu.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/billsedison/archive/2010/12/25/1916626.html">大整数类的实现 - 万户侯 - 博客园 (cnblogs.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.kedixa.top/2017/cpp-bigint-overview/">C++大整数运算（一）：概述 – kedixa的博客</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/cmj198799/article/details/6883274">RSA与大数运算（C语言）_cmj198799的博客-CSDN博客</a></li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/shoule/p/15950488.html">RSA与大数运算 - 勇敢蘑菇 - 博客园 (cnblogs.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://www.geeksforgeeks.org/bigint-big-integers-in-c-with-example/">BigInt (BIG INTEGERS) in C++ with Example - GeeksforGeeks</a></li>
</ul>
<h2 id="作者理解"><a href="#作者理解" class="headerlink" title="作者理解"></a>作者理解</h2><blockquote>
<p>注意：理解中部分基于学校计算机网络安全课程的实验要求</p>
</blockquote>
<p>可能性：</p>
<ul>
<li>能够实现自定义大数数据结构&#x2F;类及其上的相关算法</li>
<li>但是，将其嵌入RSA算法的实现，无论是对于实验量、实验要求时长，都相对复杂些</li>
</ul>
<p>复杂性的体现：</p>
<ul>
<li>如果使用相对面向数学计算、大数据应用的语言，如Python，直接调现有的库，可能认为无所不能。需要学习使用库的成本，相对的，就没有对“自定义大数结构和运算”这一“造轮子、拧螺丝”的底层理解</li>
<li>如果使用面向底层原理的语言，如C和C++，可能也有库可以直接调用，但可能没有Python那么方便使用</li>
<li>如果想自定义大数结构和运算，就需要对底层如语言支持的数据类型有一定的规划和了解。设计的这个大数结构和运算必须能够在RSA算法中被应用，而RSA算法中稍复杂的数学算法更会限制这个大数类的设计。RSA算法中，对简单数的操作已经相对复杂，再进一步对大数操作，复杂度就提升了</li>
<li>搜索网上单纯对大数的自定义代码实现（C和C++语言），几百行快至千行还是有的，再针对RSA算法进行重构，代码量会更多</li>
</ul>
<p>可能的实现：</p>
<p>1.使用十进制数数组&#x2F;向量表示大数。大数的各个位是数组中的一个元素</p>
<ul>
<li>实验要求：1024位的参数p，1024位的参数q，可能为2048位的参数n</li>
<li>实验要求需要使用二进制进行运算，另外，相应数论中使用二进制相对十进制有更简便快捷的算法</li>
<li>1024位二进制约为309位十进制数，2048位不太清楚。也就是说，十进制数组初始化大小是不太确定的。就C++而言，向量最好初始化大小，否则在未定义大小的基础上不断添加元素，可能会造成预先分配的向量空间不够，出现越界、向量空间扩容重分配等未知不好定位问题</li>
<li>几百位十进制的大数运算，在加法进位、减法借位、乘法等运算中，需要对每位操作，嵌套循环、循环次数、记录借位标志等情况很复杂</li>
</ul>
<p>2.使用二进制数数组&#x2F;向量表示大数。大数的各个位是数组中的一个元素</p>
<ul>
<li>能够满足实验要求的1024位</li>
<li>但对于可能的2048位参数n，可能也不明确向量的初始化大小</li>
<li>千位二进制相对百位十进制，定义每位操作，嵌套循环、循环次数、记录借位标志等情况会更加复杂</li>
</ul>
<p>3.使用2^32进制数组&#x2F;向量表示大数。大数的各个位是数组中的一个元素</p>
<ul>
<li>是最可能实现的方案</li>
<li>要求1024位二进制</li>
<li>语言的内置类型范围最多为64位二进制，考虑到乘法运算会使位数扩大，如超过64位而表示不了，则折中选取32位二进制，可用int数据类型表示</li>
<li>再考虑到参数&gt;0，可用unsigned int数据类型表示</li>
<li>即：相对于1.使用十进制，2.使用二进制，该方案使用2^32进制。数组中一个0~9范围内的数字，它的基数为2^32</li>
<li>1024 ÷ 32 &#x3D; 32，则参数pq的数组&#x2F;向量长度缩减为32，大大简化各种运算时的操作</li>
<li>但是，基数为2^32，则在语言的表现层面，最小操作粒度是2^32。比如用unsigned int数据类型的数字0表示0 × 2^32的大数，用unsigned int数据类型的数字1表示1 × 2^32的大数。而数字0-1即大数0-2^32的间的各种数字，在宏观层面应该是操作不到、不好操作的</li>
<li>再考虑到RSA算法中，可以使用大数运算求得p、q、n参数。但是，对于后续细粒度的n - 1、n的欧拉函数 &#x3D; (p - 1)(q - 1) &#x3D; p × q + p + q - 1等对较小数1，应该是操作不到、不好操作的</li>
<li>更不用考虑后续RSA其他数学算法了</li>
</ul>
<p>总结：</p>
<ul>
<li>可能性：大数可以实现，但不好实现。可以调库也可以自定义</li>
<li>复杂度：纯大数相对简单，考虑编程语言、为了适应RSA算法，大数结构和运算的设计相对复杂</li>
<li>可能的实现：方案都可能可以实现，综合考虑实验目的、实验量和实验时长等问题，成本可能会很高</li>
</ul>
<hr>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><h2 id="rsa-h"><a href="#rsa-h" class="headerlink" title="rsa.h"></a>rsa.h</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> RSA_RSA_H_</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> RSA_RSA_H_</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span> <span class="comment">//cout、endl、string</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span>   <span class="comment">// vector</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::string;</span><br><span class="line"><span class="keyword">using</span> std::vector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RSA</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">RSA</span>();                                                                            <span class="comment">// 构造</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">Encrypt</span><span class="params">(<span class="type">const</span> string &amp;plaintext_str, vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; &amp;ciphertext_int)</span></span>;  <span class="comment">// 加密</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">Decrypt</span><span class="params">(<span class="type">const</span> vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; &amp;ciphertext_int, string &amp;plaintext_str1)</span></span>; <span class="comment">// 解密</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">KeyGen</span><span class="params">()</span></span>; <span class="comment">// 密钥生成</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">GetPrimeNum</span><span class="params">()</span></span>;                                                                    <span class="comment">// 获取素数</span></span><br><span class="line">    <span class="function"><span class="type">bool</span> <span class="title">PrimalityTest</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;n, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a)</span></span>;                              <span class="comment">// Miller-Rabin素性测试</span></span><br><span class="line">    <span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">QuickPowMod</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;q, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;n)</span></span>; <span class="comment">// 蒙哥马利快速幂模运算</span></span><br><span class="line">    <span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">QuickMulMod</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;c)</span></span>; <span class="comment">// 快速乘模</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">ExGcd</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b, <span class="type">unsigned</span> <span class="type">int</span> &amp;x, <span class="type">unsigned</span> <span class="type">int</span> &amp;y)</span></span>; <span class="comment">// 扩展欧几里得算法</span></span><br><span class="line">    <span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">GetMulInverse</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b)</span></span>;                           <span class="comment">// 求乘法逆元</span></span><br><span class="line"></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> p_arg_; <span class="comment">// p参数</span></span><br><span class="line">    <span class="comment">// 提示：参数&gt;=0，使用unsigned int更符合语义</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> q_arg_;            <span class="comment">// q参数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> n_arg_;            <span class="comment">// n参数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> n_Euler_func_arg_; <span class="comment">// n的欧拉函数参数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> e_arg_;            <span class="comment">// e参数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> d_arg_;            <span class="comment">// d参数</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">// RSA_RSA_H_</span></span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="rsa-cpp"><a href="#rsa-cpp" class="headerlink" title="rsa.cpp"></a>rsa.cpp</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;ctime&gt;</span>   <span class="comment">//time()</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;cstdlib&gt;</span> <span class="comment">//srand()、rand()</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;rsa.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> std::cerr;</span><br><span class="line"><span class="keyword">using</span> std::cout;</span><br><span class="line"><span class="keyword">using</span> std::endl;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 构造</span></span><br><span class="line">RSA::<span class="built_in">RSA</span>()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">KeyGen</span>(); <span class="comment">// 密钥生成</span></span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;密钥生成: \t&quot;</span> &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数p: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;p_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数q: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;q_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数n: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;n_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数n的欧拉函数: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;n_Euler_func_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数e: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;e_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;参数d: \t&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;d_arg_ &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加密</span></span><br><span class="line"><span class="comment">// 参数：字符串类型的明文，无符号整型的密文</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">RSA::Encrypt</span><span class="params">(<span class="type">const</span> string &amp;plaintext_str, vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; &amp;ciphertext_int)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;加密：\t&quot;</span> &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;字符串类型的明文：\t&quot;</span> &lt;&lt; plaintext_str &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.依据ASCII码将明文的字符串数据类型转换为无符号整数类型</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> p = <span class="number">0</span>; <span class="comment">// 明文分组   1个字符1个数字为1个明文分组</span></span><br><span class="line">    <span class="comment">// 提示：要求明文分组P &lt; 参数n，依据ASCII范围0~255必 &lt; n，不再处理</span></span><br><span class="line">    <span class="function">vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; <span class="title">plaintext_int</span><span class="params">(plaintext_str.size(), <span class="number">0</span>)</span></span>; <span class="comment">// 无符号整数类型的明文    1个字符为1个数字</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; plaintext_str.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        p = plaintext_str[i]; <span class="comment">// 注意：利用自动类型转换</span></span><br><span class="line">        plaintext_int[i] = (p);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;无符号整数类型的明文：\t&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> num : plaintext_int)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; num &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.加密</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c = <span class="number">0</span>; <span class="comment">// 密文分组   1个数字明文加密得1个数字密文，1个数字为1个密文分组</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; plaintext_int.<span class="built_in">size</span>(); ++i) <span class="comment">// 对每个明文分组，蒙哥马利快速模幂加密</span></span><br><span class="line">    &#123;</span><br><span class="line">        c = <span class="keyword">this</span>-&gt;<span class="built_in">QuickPowMod</span>(plaintext_int[i], <span class="keyword">this</span>-&gt;e_arg_, <span class="keyword">this</span>-&gt;n_arg_);</span><br><span class="line">        ciphertext_int[i] = c;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;无符号整数类型的密文：\t&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> num : ciphertext_int)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; num &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 解密</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">RSA::Decrypt</span><span class="params">(<span class="type">const</span> vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; &amp;ciphertext_int, string &amp;plaintext_str1)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;解密：\t&quot;</span> &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;无符号整数类型的密文：\t&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> num : ciphertext_int)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; num &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.解密</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> p = <span class="number">0</span>;                                           <span class="comment">// 明文分组 1个数字密文解密得1个数字明文，1个数字为1个明文分组</span></span><br><span class="line">    <span class="function">vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; <span class="title">plaintext_int</span><span class="params">(ciphertext_int.size(), <span class="number">0</span>)</span></span>; <span class="comment">// 无符号整数类型的明文    1个字符为1个数字</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; ciphertext_int.<span class="built_in">size</span>(); ++i) <span class="comment">// 对每个密文分组，蒙哥马利快速模幂解密</span></span><br><span class="line">    &#123;</span><br><span class="line">        p = <span class="keyword">this</span>-&gt;<span class="built_in">QuickPowMod</span>(ciphertext_int[i], <span class="keyword">this</span>-&gt;d_arg_, <span class="keyword">this</span>-&gt;n_arg_);</span><br><span class="line">        plaintext_int[i] = p;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;无符号整数类型的明文：\t&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> num : plaintext_int)</span><br><span class="line">    &#123;</span><br><span class="line">        cout &lt;&lt; num &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2.依据ASCII码将明文的无符号整数类型转换为字符串数据类型</span></span><br><span class="line">    <span class="type">char</span> p_str = <span class="string">&#x27;\0&#x27;</span>; <span class="comment">// 字符类型的明文分组    1个数字1个字符为1个明文分组</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; plaintext_int.<span class="built_in">size</span>(); ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        p_str = <span class="built_in">static_cast</span>&lt;<span class="type">char</span>&gt;(plaintext_int[i]); <span class="comment">// 注意：利用强制类型转换</span></span><br><span class="line">        plaintext_str1[i] = (p_str);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;字符串类型的明文：\t&quot;</span> &lt;&lt; plaintext_str1 &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 密钥生成</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">RSA::KeyGen</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 1. 选择p，q。p和q为素数，p不等于q</span></span><br><span class="line">    <span class="comment">// 注意：将随机种子提取放在循环外、相同函数外，以避免时间相近获取的随机数相同</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> seed = <span class="built_in">time</span>(<span class="literal">nullptr</span>); <span class="comment">// 随机种子</span></span><br><span class="line">    <span class="built_in">srand</span>(seed);                       <span class="comment">// 设置随机种子</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">this</span>-&gt;p_arg_ = <span class="keyword">this</span>-&gt;<span class="built_in">GetPrimeNum</span>(); <span class="comment">// 获取p参数</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;q_arg_ = <span class="keyword">this</span>-&gt;<span class="built_in">GetPrimeNum</span>(); <span class="comment">// 获取q参数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 计算n = p × q</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;n_arg_ = <span class="keyword">this</span>-&gt;p_arg_ * <span class="keyword">this</span>-&gt;q_arg_;</span><br><span class="line">    <span class="comment">// 提示：第一次写习惯中文用了×号而不是*...</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 计算n的欧拉函数 = (p - 1) × (q - 1)</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;n_Euler_func_arg_ = (<span class="keyword">this</span>-&gt;p_arg_ - <span class="number">1</span>) * (<span class="keyword">this</span>-&gt;q_arg_ - <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 选择e。 e为整数，e和n的欧拉函数互素，1 &lt; e &lt; n的欧拉函数</span></span><br><span class="line">    <span class="comment">// 选择3或17或65537，e越大相对的d越小，两值比较平衡</span></span><br><span class="line">    <span class="comment">// 注意：e和n的欧拉函数互素，不能想当然的选取</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>-&gt;n_Euler_func_arg_ % <span class="number">65537</span> != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;e_arg_ = <span class="number">65537</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>-&gt;n_Euler_func_arg_ % <span class="number">17</span> != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;e_arg_ = <span class="number">17</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">this</span>-&gt;n_Euler_func_arg_ % <span class="number">3</span> != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;e_arg_ = <span class="number">3</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="comment">// 极端几乎不可能情况</span></span><br><span class="line">    &#123;</span><br><span class="line">        cerr &lt;&lt; <span class="string">&quot;无法选取参数e&quot;</span> &lt;&lt; endl;</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE); <span class="comment">// 程序直接退出</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 5. 计算d。d × e % n的欧拉函数 = 1，d &lt; n的欧拉函数</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;d_arg_ = <span class="built_in">GetMulInverse</span>(<span class="keyword">this</span>-&gt;e_arg_, <span class="keyword">this</span>-&gt;n_Euler_func_arg_);</span><br><span class="line">    <span class="comment">// 注意：对n的欧拉函数而不是参数n</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取素数</span></span><br><span class="line"><span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">RSA::GetPrimeNum</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> random = <span class="number">0</span>;     <span class="comment">// 随机数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> random_odd = <span class="number">0</span>; <span class="comment">// 随机奇数</span></span><br><span class="line"></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> n = <span class="number">0</span>;              <span class="comment">// 素性测试的参数n 循环中需要重新初始化</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a = <span class="number">0</span>;              <span class="comment">// 素性测试的参数a</span></span><br><span class="line">    <span class="type">bool</span> primality_test_res = <span class="literal">false</span>; <span class="comment">// 一次素性测试结果    false不是素数true可能为素数</span></span><br><span class="line">    <span class="type">bool</span> prime_flag = <span class="literal">false</span>;         <span class="comment">// 素数标志，最终素性测试结果。false0不是素数，true1可能为素数</span></span><br><span class="line">    <span class="comment">// 提示：初始化在循环外的变量在循环中注意是否需要更新、重新初始化</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>) <span class="comment">// 循环</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 1.1随机取一个期望大小的奇数</span></span><br><span class="line">        <span class="comment">// 1.1.1取随机数</span></span><br><span class="line">        random = <span class="built_in">rand</span>(); <span class="comment">// 随机数 一般是4~5位数，不超过unsigned int的表示范围</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 1.1.2取奇数</span></span><br><span class="line">        <span class="keyword">if</span> (random % <span class="number">2</span> == <span class="number">0</span>) <span class="comment">// 如果是偶数，+1成为奇数</span></span><br><span class="line">        &#123;</span><br><span class="line">            random_odd = random + <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="comment">// 奇数不额外操作</span></span><br><span class="line">        &#123;</span><br><span class="line">            random_odd = random;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 1.2使用素性测试判断</span></span><br><span class="line">        n = random_odd;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">128</span>; ++i) <span class="comment">// 选取128个参数a，测试128次</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">//  1.2.1随机选择相关参数a。满足a为整数，1 &lt; a &lt; n - 1</span></span><br><span class="line">            a = <span class="built_in">rand</span>() % (n - <span class="number">1</span>); <span class="comment">// 0 ~ n - 2</span></span><br><span class="line">            <span class="comment">// 注意：</span></span><br><span class="line">            <span class="comment">// 因为运行时间段相近，第一次a取的随机数可能和n相等</span></span><br><span class="line">            <span class="comment">// 则计算后结果必为1，而后1 + 1 = 2</span></span><br><span class="line">            <span class="comment">// 将设置随机种子代码提取出函数后，排除该错误</span></span><br><span class="line">            <span class="keyword">if</span> (a == <span class="number">0</span>) <span class="comment">// 如果是0，令a = 2 &gt; 1</span></span><br><span class="line">            &#123;</span><br><span class="line">                a += <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (a == <span class="number">1</span>) <span class="comment">// 如果是1，令a = 2 &gt; 1</span></span><br><span class="line">            &#123;</span><br><span class="line">                ++a;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            primality_test_res = <span class="built_in">PrimalityTest</span>(random_odd, a); <span class="comment">// 素性测试</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (primality_test_res == <span class="literal">true</span>) <span class="comment">// 一次测试结果可能为素数</span></span><br><span class="line">            &#123;</span><br><span class="line">                prime_flag = <span class="literal">true</span>; <span class="comment">// 标记可能为素数</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (primality_test_res == <span class="literal">false</span>) <span class="comment">// 只要有一次素性测试不是素数，则必不为素数</span></span><br><span class="line">            &#123;</span><br><span class="line">                prime_flag = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">break</span>; <span class="comment">// 不再用a测试，需要重新选取随机奇数</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (prime_flag == <span class="literal">true</span>) <span class="comment">// 随机奇数可能为素数，</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">break</span>; <span class="comment">// 退出循环</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 否则随机奇数不是素数，进入循环，再重新进行1.1取随机奇数，1.2素性测试步骤</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> random_odd; <span class="comment">// 获得素数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Miller-Rabin素性测试</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">RSA::PrimalityTest</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;n, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a)</span> <span class="comment">// 参数：随机奇数，参数a</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 1.2.2找到相关参数k，q。满足n - 1 = 2 ^ k × q。k、q为整数，k &gt; 0，q为奇数</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> k = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> q = n - <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提示：</span></span><br><span class="line">    <span class="comment">// 很多算法都只说明要找到k、q，却不说怎么找</span></span><br><span class="line">    <span class="comment">// 找k，q的代码也含糊其辞的</span></span><br><span class="line">    <span class="keyword">while</span> ((q &amp; <span class="number">1</span>) == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        ++k;</span><br><span class="line">        q &gt;&gt;= <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 理解：</span></span><br><span class="line">    <span class="comment">// q &amp; 1：即q的二进制表示与二进制位1与运算，取q二进制表示的最低位0或1</span></span><br><span class="line">    <span class="comment">// 如101 &amp; 1 = 101 &amp; 001 = 001 = 1</span></span><br><span class="line">    <span class="comment">// 如0010 &amp; 1 = 0010 &amp; 0001 = 0</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在最低位中，基数2 ^ 0 = 1，所以如果值是0，则1 × 0 = 0为偶数；值是1，则1 × 1 = 1为奇数</span></span><br><span class="line">    <span class="comment">// 所以，如果运算结果为0，则是偶数，可以提取一个因子2</span></span><br><span class="line">    <span class="comment">// while：连续提取因子2</span></span><br><span class="line">    <span class="comment">// 每提取一个因子2，则++k，k是因子2的计数</span></span><br><span class="line">    <span class="comment">// q &gt;&gt;= 1：将q的二进制表示右移缩小，继续对最低位判断提取因子2</span></span><br><span class="line">    <span class="comment">// 直到不能连续提取因子2，则q即为所求</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如十进制13 - 1 = 12 = 二进制1100，在第1、2位提取因子2为2 ^ 2 = 4</span></span><br><span class="line">    <span class="comment">// 所以12 = 2 ^ 2 × 3。k = 2，q = 3</span></span><br><span class="line">    <span class="comment">// 如十进制7 - 1 = 6 = 二进制110，在第1位提取1个因子2为2 ^ 1 = 2</span></span><br><span class="line">    <span class="comment">// 所以6 = 2 ^ 1 × 3。k = 1，q = 3</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提示：注意k、q的取值条件</span></span><br><span class="line">    <span class="comment">// 对正整数素数，除了2为偶数，其他数必为奇数</span></span><br><span class="line">    <span class="comment">// 奇数-1必为偶数，必至少能提取1个公因子2，则k至少为1 &gt; 0满足</span></span><br><span class="line">    <span class="comment">// 由算法性质，知提取所有的公因子2，则结果q必为奇数满足</span></span><br><span class="line">    <span class="comment">// 一般q数很大，所以在接下来的步骤需要用蒙哥马利快速模幂算法</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.2.3计算a ^ q % n</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> aq_mod_n = <span class="keyword">this</span>-&gt;<span class="built_in">QuickPowMod</span>(a, q, n);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// cout &lt;&lt; n &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">// cout &lt;&lt; k &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">// cout &lt;&lt; q &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">// cout &lt;&lt; a &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">// cout &lt;&lt; aq_mod_n &lt;&lt; endl;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.2.4运用二次探测定理的逆否命题判断</span></span><br><span class="line">    <span class="comment">// 正命题大概：探测，所有解只有1或n-1，则可能为素数</span></span><br><span class="line">    <span class="comment">// 逆否命题大概：探测，存在解不为1且不为n-1，则必定不是素数</span></span><br><span class="line">    <span class="comment">// 可以用正命题也可以用逆否命题判断。以下用正命题和逆否命题判断</span></span><br><span class="line">    <span class="comment">// 第一个判断条件：未探测时，a ^ q % n == 1，则可能为素数</span></span><br><span class="line">    <span class="keyword">if</span> (aq_mod_n == <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 第二个判断条件：二次探测时，只要存在不为1且不为n-1，则必定不是素数</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; k; ++j) <span class="comment">// 0 ~ k-1</span></span><br><span class="line">    &#123;</span><br><span class="line">        aq_mod_n = <span class="keyword">this</span>-&gt;<span class="built_in">QuickPowMod</span>(aq_mod_n, <span class="number">2</span>, n);</span><br><span class="line">        <span class="comment">// 对序列二次探测 计算a ^ (q × 2 ^ j) % n = aq_mod_n ^ (2 ^ j) % n。每次循环都幂2相当于(2 ^ j)</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (aq_mod_n != <span class="number">1</span> &amp;&amp; aq_mod_n != n - <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">// 第二个判断条件：二次探测时，若没有因判断为合数而返回，则可能为素数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 蒙哥马利快速幂模运算</span></span><br><span class="line"><span class="comment">// 参数：a ^ q % n</span></span><br><span class="line"><span class="comment">// 返回值：a ^ q % n</span></span><br><span class="line"><span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">RSA::QuickPowMod</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;q, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 原理：</span></span><br><span class="line">    <span class="comment">//  幂运算性质：a ^ q = a ^ q1 × a ^ q2。q = q1 + q2</span></span><br><span class="line">    <span class="comment">//  模运算性质：(a × b) % n = [(a % n) × (b % n)] % n</span></span><br><span class="line">    <span class="comment">// 所以：a ^ q % n = (a ^ q1 × a ^ q2) % n = [(a ^ q1 % n) × (a ^ q2 % n)] % n</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> res = <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a_temp = a; <span class="comment">// 运算中会改变a的值，暂存</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> q_temp = q; <span class="comment">// 运算中会改变q的值，暂存</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提示：很多算法代码含糊其辞的</span></span><br><span class="line">    <span class="keyword">while</span> (q_temp &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> ((q_temp &amp; <span class="number">1</span>) == <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            res = <span class="keyword">this</span>-&gt;<span class="built_in">QuickMulMod</span>(res, a_temp, n);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        a_temp = <span class="keyword">this</span>-&gt;<span class="built_in">QuickMulMod</span>(a_temp, a_temp, n);</span><br><span class="line"></span><br><span class="line">        q_temp &gt;&gt;= <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 理解：</span></span><br><span class="line">    <span class="comment">// 算法是针对十进制数的二进制表示进行运算的</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// while (q_temp &gt; 0)：对比素性测试的内容：while ((q &amp; 1) == 0)</span></span><br><span class="line">    <span class="comment">// 这里是判断值，需要判断所有二进制位，所以只要q在后面的右移位中值不为0，就循环。而素性测试中是判断位</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// if ((q_temp &amp; 1) == 1)：最低位为1时，该位有效，需要计算并更新结果</span></span><br><span class="line">    <span class="comment">// 快速乘算法：res = (res × a_temp) % n</span></span><br><span class="line">    <span class="comment">// 该步骤相当于每次计算单个的(a ^ q2 % n)，然后和之前的(a ^ q1 % n)相乘作为新的结果</span></span><br><span class="line">    <span class="comment">// 其中第一个res是更新结果，第二个res是之前的结果，a_temp是当前的基数</span></span><br><span class="line">    <span class="comment">// 基数：在循环中对每一位都会更新基数（见后面步骤），在二进制表示为1时，该基数有效</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// a_temp = QuickMulMod(a_temp, a_temp, n);相当于a_temp = a_temp × a_temp % n</span></span><br><span class="line">    <span class="comment">// 如初始a_temp = 2，则不断更新为2 ^ 0 = 1，2 ^ 1 = 2</span></span><br><span class="line">    <span class="comment">// 再进行%保证基数不超过范围</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 快速乘</span></span><br><span class="line"><span class="comment">// 参数：a * b % c</span></span><br><span class="line"><span class="comment">// 返回值：a * b % c</span></span><br><span class="line"><span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">RSA::QuickMulMod</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 原理：</span></span><br><span class="line">    <span class="comment">// 同快速幂模运算，将乘法转换为加法运算</span></span><br><span class="line">    <span class="comment">// a × b % c = [(a + a) % c] + [(a + a) % c] + ... [(a + a) % c]共b个a相加求模</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> res = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a_temp = a;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b_temp = b;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (b_temp &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (b_temp &amp; <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            res = (res + a_temp) % c;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        a_temp = (a_temp + a_temp) % c;</span><br><span class="line"></span><br><span class="line">        b_temp &gt;&gt;= <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 扩展欧几里得算法</span></span><br><span class="line"><span class="comment">// 参数和返回值：</span></span><br><span class="line"><span class="comment">// 由欧几里得算法，求两数a和b，a &gt;= b的最大公约数g</span></span><br><span class="line"><span class="comment">// 由贝祖定理，存在令a × x + b × y = g的解x和y</span></span><br><span class="line"><span class="comment">// 由扩展欧几里得算法，求令a × x + b × y = gcd的一组解x，y</span></span><br><span class="line"><span class="comment">// 该组解x和y随后用于求乘法逆元</span></span><br><span class="line"><span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">RSA::ExGcd</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b, <span class="type">unsigned</span> <span class="type">int</span> &amp;x, <span class="type">unsigned</span> <span class="type">int</span> &amp;y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 大致思路：</span></span><br><span class="line">    <span class="comment">//  由欧几里得算法，求a和b的最大公约数：gcd(a，b) = gcd(b，a % b)</span></span><br><span class="line">    <span class="comment">//  所以是个递归过程，递归出口为右数b，即右数a % b = 0，此时左数a即为最大公约数返回</span></span><br><span class="line">    <span class="comment">//  此时即：gcd(g，0) = g = a × x + b × y = a × 1 + b × 0</span></span><br><span class="line">    <span class="comment">//  即在递归栈顶时，求得解x = 1，y = 0</span></span><br><span class="line">    <span class="comment">//  随后逐层返回退出递归栈，由扩展欧几里得算法推导，将x和y逐层更新，最后求得解x和y</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 递归出口</span></span><br><span class="line">    <span class="keyword">if</span> (b == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        x = <span class="number">1</span>;</span><br><span class="line">        y = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> a;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 递归逻辑</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> g = <span class="built_in">ExGcd</span>(b, a % b, x, y); <span class="comment">// g是当前递归的最大公约数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 提示：</span></span><br><span class="line">    <span class="comment">// 将x和y从栈底到栈顶更新，即正向写代码逻辑递归</span></span><br><span class="line">    <span class="comment">// 则返回时从栈顶到栈底更新，最后求得解</span></span><br><span class="line">    <span class="type">int</span> temp = y;</span><br><span class="line">    y = x - (a / b) * y;</span><br><span class="line">    x = temp;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> g; <span class="comment">// 返回最大公约数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 求乘法逆元</span></span><br><span class="line"><span class="comment">// 参数：a × x % b = 1的a和b</span></span><br><span class="line"><span class="comment">// 返回值：a模b的乘法逆元x</span></span><br><span class="line"><span class="function"><span class="type">unsigned</span> <span class="type">int</span> <span class="title">RSA::GetMulInverse</span><span class="params">(<span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;a, <span class="type">const</span> <span class="type">unsigned</span> <span class="type">int</span> &amp;b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 大致思路：</span></span><br><span class="line">    <span class="comment">// 若求a模b的乘法逆元x，即a × x % b = 1</span></span><br><span class="line">    <span class="comment">//  即同余式：ax ≡ 1(mod b)</span></span><br><span class="line">    <span class="comment">//  可转换为不定方程：a × x + b × y = 1</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对两个素数a，b，最大公约数为1，即gcd(a，b) = 1</span></span><br><span class="line">    <span class="comment">// 所以：a × x + b × y = 1 = gcd(a，b)</span></span><br><span class="line">    <span class="comment">// 由贝祖定理，存在令a × x + b × y = gcd(a，b)的解x和y</span></span><br><span class="line">    <span class="comment">// 即已知a和b，求解x</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 所以：</span></span><br><span class="line">    <span class="comment">// 使用扩展欧几里得算法求a × x + b × y = 1的解x</span></span><br><span class="line">    <span class="comment">// 由同余式，逆元z=(x % b + b) % b</span></span><br><span class="line"></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> x = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> y = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> g = <span class="keyword">this</span>-&gt;<span class="built_in">ExGcd</span>(a, b, x, y); <span class="comment">// 使用扩展欧几里得算法求a × x + b × y = 1的解x</span></span><br><span class="line"></span><br><span class="line">    x = (x % b + b) % b;</span><br><span class="line">    <span class="comment">// 提示：</span></span><br><span class="line">    <span class="comment">// x是a模b的乘法逆元，但不能保证是正数，不能保证落在(a,b)内，所以需要更新</span></span><br><span class="line">    <span class="comment">// 这一步很多讲解都含糊其辞</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 思路：如果求得x为负数，则需要转换为正数，由模运算性质保证结果不变</span></span><br><span class="line">    <span class="comment">// 假设x = -9，b = 20</span></span><br><span class="line">    <span class="comment">// 由模运算性质：对负数-9 ≡ -9 % 20 ≡ 对正数x1 % 20</span></span><br><span class="line">    <span class="comment">// 想办法将负数-9转换为正数x1，且保证模后结果恒等</span></span><br><span class="line">    <span class="comment">// 将20转换为负数，再加上一个因子构造-9，即[20 × (-1) + 11] % 20 = -9</span></span><br><span class="line">    <span class="comment">// 由模运算性质：-9 = -9 % 20= [20 × (-1) + 11] % 20 = &#123;[(-20) % 20] + (11 % 20)&#125; % 20 = [0 + (11 % 20)] % 20  = 11 % 20 = 11</span></span><br><span class="line">    <span class="comment">// 所以新更新的x1为11</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// x % b：缩小负数在(-b，0]范围</span></span><br><span class="line">    <span class="comment">// 加上1个b能够一次性转换为正数，范围在[0，b]</span></span><br><span class="line">    <span class="comment">// 注意如果x % b = 0时，+b = b，还需要% b使范围落在[0，b)</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> x;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="main-cpp"><a href="#main-cpp" class="headerlink" title="main.cpp"></a>main.cpp</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;rsa.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    RSA *rsa = <span class="keyword">new</span> RSA;</span><br><span class="line"></span><br><span class="line">    <span class="function">string <span class="title">plaintext_str</span><span class="params">(<span class="string">&quot;yezhening&quot;</span>)</span></span>;                            <span class="comment">// 字符串类型的明文</span></span><br><span class="line">    <span class="function">vector&lt;<span class="type">unsigned</span> <span class="type">int</span>&gt; <span class="title">ciphertext_int</span><span class="params">(plaintext_str.size(), <span class="number">0</span>)</span></span>; <span class="comment">// 无符号整数类型的密文</span></span><br><span class="line">    <span class="function">string <span class="title">plaintext_str1</span><span class="params">(plaintext_str.size(), <span class="string">&#x27;\0&#x27;</span>)</span></span>;            <span class="comment">// 字符串类型的明文   解密后的明文</span></span><br><span class="line"></span><br><span class="line">    rsa-&gt;<span class="built_in">Encrypt</span>(plaintext_str, ciphertext_int);  <span class="comment">// 加密得密文</span></span><br><span class="line">    rsa-&gt;<span class="built_in">Decrypt</span>(ciphertext_int, plaintext_str1); <span class="comment">// 解密得明文</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">delete</span> rsa;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h1><p><img src= "" data-lazy-src="/../img/rsa/0ecb9dc979534c7e95494cc65565a708.png" alt="请添加图片描述"></p>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>网络安全中RSA的C++语言描述简单实现。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li>《密码编码学与网络安全——原理与实践(第五版)》作者：William Stallings</li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/kentle/p/14975056.html">Miller-Rabin 素性检测 - kentle - 博客园 (cnblogs.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/forever_dreams/article/details/82314237">Miller-Rabin素数测试算法_forever_dreams的博客-CSDN博客_millerrabin素数测试算法</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/heshiip/article/details/95679397">素性测试的Miller-Rabin算法完全解析 （C语言实现、Python实现）_heshiip的博客-CSDN博客_millerrabin算法c代码</a></li>
<li><a target="_blank" rel="noopener" href="https://zpf1900.blog.csdn.net/article/details/85197424?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-85197424-blog-51457540.pc_relevant_landingrelevant&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-85197424-blog-51457540.pc_relevant_landingrelevant&utm_relevant_index=2">米勒-拉宾素性检验(MillerRabbin)算法详解_1900_的博客-CSDN博客_米勒拉宾素性检验</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/lovecyr/article/details/105372427">扩展欧几里得算法（详细推导+代码实现+应用）_胡小涛的博客-CSDN博客_扩展欧几里得算法代码</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_43772166/article/details/104254604">求解乘法逆元的方法_默_silence的博客-CSDN博客_求乘法逆元</a></li>
<li><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/370615983">计算乘法逆元 - 知乎 (zhihu.com)</a></li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2022/11/09/RSA%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/">http://example.com/2022/11/09/RSA的C++语言描述简单实现/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/">网络安全</a></div><div class="post_share"><div class="social-share" data-image="/img/cover15.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/10/24/%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86%EF%BC%88DES%EF%BC%89%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0/" title="数据加密标准（DES）的C++语言描述实现"><img class="cover" src= "" data-lazy-src="/img/cover10.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">数据加密标准（DES）的C++语言描述实现</div></div></a></div><div class="next-post pull-right"><a href="/2022/11/16/Diffie-Hellman%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" title="Diffie-Hellman的C++语言描述简单实现"><img class="cover" src= "" data-lazy-src="/img/cover6.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Diffie-Hellman的C++语言描述简单实现</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/16/Diffie-Hellman%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" title="Diffie-Hellman的C++语言描述简单实现"><img class="cover" src= "" data-lazy-src="/img/cover6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-16</div><div class="title">Diffie-Hellman的C++语言描述简单实现</div></div></a></div><div><a href="/2022/10/24/%E6%95%B0%E6%8D%AE%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86%EF%BC%88DES%EF%BC%89%E7%9A%84C++%E8%AF%AD%E8%A8%80%E6%8F%8F%E8%BF%B0%E5%AE%9E%E7%8E%B0/" title="数据加密标准（DES）的C++语言描述实现"><img class="cover" src= "" data-lazy-src="/img/cover10.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-10-24</div><div class="title">数据加密标准（DES）的C++语言描述实现</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">代码仓库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E7%89%B9%E7%82%B9"><span class="toc-number">3.</span> <span class="toc-text">代码特点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%A7%EF%BC%88%E7%B4%A0%EF%BC%89%E6%95%B0%E8%AE%A8%E8%AE%BA"><span class="toc-number">4.</span> <span class="toc-text">大（素）数讨论</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%83%A8%E5%88%86%E8%B5%84%E6%96%99"><span class="toc-number">4.1.</span> <span class="toc-text">部分资料</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%90%86%E8%A7%A3"><span class="toc-number">4.2.</span> <span class="toc-text">作者理解</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">5.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#rsa-h"><span class="toc-number">5.1.</span> <span class="toc-text">rsa.h</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#rsa-cpp"><span class="toc-number">5.2.</span> <span class="toc-text">rsa.cpp</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#main-cpp"><span class="toc-number">5.3.</span> <span class="toc-text">main.cpp</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%93%E6%9E%9C"><span class="toc-number">6.</span> <span class="toc-text">结果</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">7.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">8.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">9.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover15.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>